package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JxRetakeScheduleMapper;
import com.ruoyi.system.domain.JxRetakeSchedule;
import com.ruoyi.system.service.IJxRetakeScheduleService;

/**
 * 补考安排Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class JxRetakeScheduleServiceImpl implements IJxRetakeScheduleService 
{
    @Autowired
    private JxRetakeScheduleMapper jxRetakeScheduleMapper;

    /**
     * 查询补考安排
     * 
     * @param retakeId 补考安排主键
     * @return 补考安排
     */
    @Override
    public JxRetakeSchedule selectJxRetakeScheduleByRetakeId(Long retakeId)
    {
        return jxRetakeScheduleMapper.selectJxRetakeScheduleByRetakeId(retakeId);
    }

    /**
     * 查询补考安排列表
     * 
     * @param jxRetakeSchedule 补考安排
     * @return 补考安排
     */
    @Override
    public List<JxRetakeSchedule> selectJxRetakeScheduleList(JxRetakeSchedule jxRetakeSchedule)
    {
        return jxRetakeScheduleMapper.selectJxRetakeScheduleList(jxRetakeSchedule);
    }

    /**
     * 新增补考安排
     * 
     * @param jxRetakeSchedule 补考安排
     * @return 结果
     */
    @Override
    public int insertJxRetakeSchedule(JxRetakeSchedule jxRetakeSchedule)
    {
        jxRetakeSchedule.setCreateTime(DateUtils.getNowDate());
        jxRetakeSchedule.setCreateBy(SecurityUtils.getUsername());
        return jxRetakeScheduleMapper.insertJxRetakeSchedule(jxRetakeSchedule);
    }

    /**
     * 修改补考安排
     * 
     * @param jxRetakeSchedule 补考安排
     * @return 结果
     */
    @Override
    public int updateJxRetakeSchedule(JxRetakeSchedule jxRetakeSchedule)
    {
        jxRetakeSchedule.setUpdateTime(DateUtils.getNowDate());
        jxRetakeSchedule.setUpdateBy(SecurityUtils.getUsername());
        return jxRetakeScheduleMapper.updateJxRetakeSchedule(jxRetakeSchedule);
    }

    /**
     * 批量删除补考安排
     * 
     * @param retakeIds 需要删除的补考安排主键
     * @return 结果
     */
    @Override
    public int deleteJxRetakeScheduleByRetakeIds(Long[] retakeIds)
    {
        return jxRetakeScheduleMapper.deleteJxRetakeScheduleByRetakeIds(retakeIds);
    }

    /**
     * 删除补考安排信息
     * 
     * @param retakeId 补考安排主键
     * @return 结果
     */
    @Override
    public int deleteJxRetakeScheduleByRetakeId(Long retakeId)
    {
        return jxRetakeScheduleMapper.deleteJxRetakeScheduleByRetakeId(retakeId);
    }

    /**
     * 查询学员的待考试补考安排
     * 
     * @param studentId 学员ID
     * @param subjectId 科目ID
     * @return 补考安排
     */
    @Override
    public JxRetakeSchedule selectPendingRetakeByStudentAndSubject(Long studentId, Long subjectId)
    {
        return jxRetakeScheduleMapper.selectPendingRetakeByStudentAndSubject(studentId, subjectId);
    }

    /**
     * 查询学员所有待考试的补考安排
     * 
     * @param studentId 学员ID
     * @return 补考安排列表
     */
    @Override
    public List<JxRetakeSchedule> selectPendingRetakesByStudent(Long studentId)
    {
        return jxRetakeScheduleMapper.selectPendingRetakesByStudent(studentId);
    }

    /**
     * 查询指定日期范围内的补考安排
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 补考安排列表
     */
    @Override
    public List<JxRetakeSchedule> selectRetakeScheduleByDateRange(String startDate, String endDate)
    {
        return jxRetakeScheduleMapper.selectRetakeScheduleByDateRange(startDate, endDate);
    }

    /**
     * 自动生成补考安排
     * 
     * @param studentId 学员ID
     * @param subjectId 科目ID
     * @param examTime 考试时间
     * @return 结果
     */
    @Override
    public int autoGenerateRetakeSchedule(Long studentId, Long subjectId, Date examTime)
    {
        // 检查是否已存在待考试的补考安排
        JxRetakeSchedule existingRetake = selectPendingRetakeByStudentAndSubject(studentId, subjectId);
        if (existingRetake != null) {
            return 0; // 已存在，不重复创建
        }

        JxRetakeSchedule retakeSchedule = new JxRetakeSchedule();
        retakeSchedule.setStudentId(studentId);
        retakeSchedule.setSubjectId(subjectId);
        retakeSchedule.setScheduledTime(examTime);
        retakeSchedule.setStatus("待考试");
        retakeSchedule.setRemark("系统自动安排");

        return insertJxRetakeSchedule(retakeSchedule);
    }

    /**
     * 完成补考安排
     * 
     * @param retakeId 补考安排ID
     * @return 结果
     */
    @Override
    public int completeRetakeSchedule(Long retakeId)
    {
        JxRetakeSchedule retakeSchedule = selectJxRetakeScheduleByRetakeId(retakeId);
        if (retakeSchedule != null) {
            retakeSchedule.setStatus("已完成");
            retakeSchedule.setUpdateTime(DateUtils.getNowDate());
            retakeSchedule.setUpdateBy(SecurityUtils.getUsername());
            return updateJxRetakeSchedule(retakeSchedule);
        }
        return 0;
    }

    /**
     * 取消补考安排
     * 
     * @param retakeId 补考安排ID
     * @return 结果
     */
    @Override
    public int cancelRetakeSchedule(Long retakeId)
    {
        JxRetakeSchedule retakeSchedule = selectJxRetakeScheduleByRetakeId(retakeId);
        if (retakeSchedule != null) {
            retakeSchedule.setStatus("已取消");
            retakeSchedule.setUpdateTime(DateUtils.getNowDate());
            retakeSchedule.setUpdateBy(SecurityUtils.getUsername());
            return updateJxRetakeSchedule(retakeSchedule);
        }
        return 0;
    }
}
